package com.vrv.netmgr.remote;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vrv.common.utils.StringUtil;
import com.vrv.framework.remote.RestfulRemoteService;
import com.vrv.framework.vo.Page;
import com.vrv.framework.vo.SearchCondition;
import com.vrv.netmgr.entity.Classes;
import com.vrv.netmgr.entity.DeviceInfo;
import com.vrv.netmgr.entity.DistributeType;
import com.vrv.netmgr.entity.Policy;
import com.vrv.netmgr.service.ClassesService;
import com.vrv.netmgr.service.DeviceInfoService;
import com.vrv.netmgr.service.DistributeTypeService;
import com.vrv.netmgr.service.PolicyService;
import com.vrv.netmgr.vo.PolicySearchCondition;

/**
 * PolicyListController对象
 *
 * @author 曹保利
 * @version 1.0
 */
@Controller
@RequestMapping("/policy/*")
public class PolicyRemote extends RestfulRemoteService<Policy, PolicySearchCondition> {

    @Autowired
    public void setPolicyService(PolicyService service) {
        setEntityService(service);
    }

    @Autowired
    private PolicyService PolicyService;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private DeviceInfoService deviceInfoService;

    @Autowired
    private DistributeTypeService distributeTypeService;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void decorateSearchResult(Page page, SearchCondition searchCondition) {

        List<Object> objects = page.getResult();

        for (Object obj : objects) {
            Policy policy = (Policy) obj;
            String content = policy.getPolicyObject();

            // 解析策略对象
            Document doc = Jsoup.parse(content);

            String classId = doc.select("ClassID").text();
            String deviceIds = doc.select("DeviceID").text();
            String ipRange = doc.select("IPRange").text();
            String osType = doc.select("OSType").text();
            String group = doc.select("GroupID").text();
            String office = doc.select("DANWEINAME").text();
            String department = doc.select("DEPTNAME").text();
            String migrate = doc.select("MIGRATE").text();
            String tempClient = doc.select("TEMPCLIENT").text();
            String allDevice = doc.select("ALLDEVICE").text();

            String object = "";

            if (StringUtil.isValidString(classId)) {
                if (classId.contains(";")) {
                    String[] ids = classId.split(";");
                    List<Classes> classes = (List<Classes>) classesService.get(ids).getResultObject();
                    String classNames = "";
                    for (int i = 0; (i < classes.size() && classes != null); i++) {
                        if (null != classes && StringUtil.isValidString(classes.get(i).getClassName())) {
                            classNames = classNames + " ; " + classes.get(i).getClassName();
                        }
                    }
                    object = object + "[区域]:" + classNames + "\n";
                } else {
                    Classes classes = (Classes) classesService.get(classId).getResultObject();
                    if (null != classes && StringUtil.isValidString(classes.getClassName())) {
                        object = object + "[区域]:" + classes.getClassName() + "\n";
                    }
                }
            }
            if (StringUtil.isValidString(deviceIds)) {
                if (deviceIds.contains(",")) {
                    String[] ids = deviceIds.split(",");
                    List<DeviceInfo> deviceInfoes = (List<DeviceInfo>) deviceInfoService.get(ids).getResultObject();
                    String deviceInfoName = "";
                    for (int i = 0; (deviceInfoes != null && i < deviceInfoes.size()); i++) {
                        if (null != deviceInfoes && StringUtil.isValidString(deviceInfoes.get(i).getDeviceName())) {
                            deviceInfoName = deviceInfoName + deviceInfoes.get(i).getIpaddres() + ":" + deviceInfoes.get(i).getDeviceName() + ";\n";
                        }
                    }
                    object = object + "[设备]:\n" + deviceInfoName + "\n";
                } else {
                    DeviceInfo deviceInfo = (DeviceInfo) deviceInfoService.get(deviceIds).getResultObject();
                    if (null != deviceInfo && StringUtil.isValidString(deviceInfo.getDeviceName())) {
                        object = object + "[设备]:\n" + deviceInfo.getIpaddres() + ":" + deviceInfo.getDeviceName() + "\n";
                    }
                }
            }
            if (StringUtil.isValidString(ipRange)) {
                object = object + "[IP区域]:\n" + ipRange + "\n";
            }
            if (StringUtil.isValidString(osType)) {
                object = object + "[系统类型]:" + osType + "\n";
            }
            if (StringUtil.isValidString(group)) {
                DistributeType distributeType = (DistributeType) distributeTypeService.get(group).getResultObject();
                if (distributeType != null) {
                    object = object + "[设备组]:" + distributeType.getTypeName() + "\n";
                }
            }
            if (StringUtil.isValidString(office)) {
                object = object + "[办公室]:" + office + "\n";
            }
            if (StringUtil.isValidString(department)) {
                object = object + "[部门]:" + department + "\n";
            }
            if (StringUtil.isValidString(migrate)) {
                object = object + "[漫游设备]\n";
            }
            if (StringUtil.isValidString(tempClient)) {
                object = object + "[临时客户端]\n";
            }
            if (content.contains("ALLDEVICE")) {
                object = object + "[所有设备]\n";
            }
            if (!StringUtil.isValidString(object)) {
                object = "尚未分配 ";
            }

            policy.setPolicyObject(object);

            // 拼组策略名
            String name = policy.getPolicyName();
            if (policy.getReserved2() > 0) {// 上级策略
                name = name + "-上级";
                if ((policy.getReserved1() & 8) > 0) {
                    name = name + "-" + "强制策略";
                } else if ((policy.getReserved1() & 16) > 0) {
                    name = name + "-" + "样板模版";
                } else if ((policy.getReserved1() & 32) > 0) {
                    name = name + "-" + "自定义模版";
                } else {
                    name = name + "-" + "普通策略";
                }
            } else {// 本机策略
                name = name + "-本级";
                if ((policy.getReserved1() & 8) > 0) {
                    name = name + "-" + "强制策略";
                } else if ((policy.getReserved1() & 16) > 0) {
                    name = name + "-" + "样板模版";
                } else if ((policy.getReserved1() & 32) > 0) {
                    name = name + "-" + "自定义模版";
                } else {
                    name = name + "-" + "普通策略";
                }
            }

            policy.setPolicyName(name);
        }
        super.decorateSearchResult(page, searchCondition);
    }
}
